package com.example.activitysign.service.impl;

import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.activitysign.entity.ActivityTaskItem;
import com.example.activitysign.mapper.ActivityTaskItemMapper;
import com.example.activitysign.mapper.ActivityTaskRelationMapper;
import com.example.activitysign.pages.PageResult;
import com.example.activitysign.pages.PageValidate;
import com.example.activitysign.service.IActivityTaskItemService;
import com.example.activitysign.validate.ActivityTaskItemCreateValidate;
import com.example.activitysign.validate.ActivityTaskItemSearchValidate;
import com.example.activitysign.validate.ActivityTaskItemUpdateValidate;
import com.example.activitysign.vo.activity.ActivityTaskItemDetailVo;
import com.example.activitysign.vo.activity.ActivityTaskItemListedVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 任务列实现类
 *
 * @author fei
 */
@Slf4j
@Service
public class ActivityTaskItemServiceImpl extends ServiceImpl<ActivityTaskItemMapper, ActivityTaskItem> implements IActivityTaskItemService {

    @Resource
    private ActivityTaskItemMapper activityTaskItemMapper;
    @Resource
    private DefaultIdentifierGenerator defaultIdentifierGenerator;
    @Resource
    private ActivityTaskRelationMapper activityTaskRelationMapper;

    /**
     * 任务列列表
     *
     * @param pageValidate   分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ActivityTaskItemListedVo>
     * @author fei
     */
    @Override
    public PageResult<ActivityTaskItemListedVo> list(PageValidate pageValidate, ActivityTaskItemSearchValidate searchValidate) {
        Page<ActivityTaskItem> page = new Page<>(pageValidate.getPageNo(), 1000);
        IPage<ActivityTaskItemListedVo> list = activityTaskItemMapper.list(page, searchValidate);
        return PageResult.iPageHandle(list.getTotal(), list.getCurrent(), list.getSize(), list.getRecords());
    }

    /**
     * 任务列详情
     *
     * @param id 主键参数
     * @return ActivityTaskItem
     * @author fei
     */
    @Override
    public ActivityTaskItemDetailVo detail(Long id) {
        ActivityTaskItem model = activityTaskItemMapper.selectById(id);
        Assert.notNull(model, "数据不存在");

        ActivityTaskItemDetailVo vo = new ActivityTaskItemDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 任务列新增
     *
     * @param createValidate 参数
     * @author fei
     */
    @Override
    public void add(ActivityTaskItemCreateValidate createValidate) {
        ActivityTaskItem model = new ActivityTaskItem();
        BeanUtils.copyProperties(createValidate, model);
        model.setId(defaultIdentifierGenerator.nextId(model));
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        activityTaskItemMapper.insert(model);
    }

    /**
     * 任务列编辑
     *
     * @param updateValidate 参数
     * @author fei
     */
    @Override
    public void edit(ActivityTaskItemUpdateValidate updateValidate) {
        ActivityTaskItem model = activityTaskItemMapper.selectById(updateValidate.getId());
        Assert.notNull(model, "数据不存在!");
        BeanUtils.copyProperties(updateValidate, model);
        model.setUpdateTime(new Date());
        activityTaskItemMapper.updateById(model);
    }

    /**
     * 任务列删除
     *
     * @param id 主键ID
     * @author fei
     */
    @Override
    public void del(Long id) {
        int i = activityTaskItemMapper.deleteById(id);

        activityTaskRelationMapper.deleteRelation(id);
        Assert.isTrue(i > 0, "数据不存在!");
    }

}

